package com.wangsd.web.api;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.utils.JiGuangUtils;
import com.wangsd.web.MyController;
import com.wangsd.web.face.entity.TOrder;
import com.wangsd.web.face.service.TOrderService;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "statisticsApi", tags = "订单统计查询接口")
@RestController
@RequestMapping("/api/statistics")
@Slf4j
public class ApiStatisticsController extends MyController {

    @Autowired
    TOrderService orderService;
    @Autowired
    SysUserService sysUserService;

    @ApiOperation(value = "查询门店分组汇总列表", notes = "查询门店分组汇总列表", httpMethod = "GET")
    @RequestMapping(value = "/getStoreOrderGroupList/{pageNum}/{pageSize}", method = RequestMethod.GET)
    @ResponseBody
    public Result getOrderList(@ApiParam(value = "当前页", required = true) @PathVariable("pageNum") Integer pageNum,
                               @ApiParam(value = "每页大小", required = true) @PathVariable("pageSize") Integer pageSize,
                               @ApiParam(value = "查询日期（2011-01-20）", required = true) @RequestParam String queryTime,
                               @ApiParam(value = "查询类型（0日账单，1月账单，2年账单）", required = true) @RequestParam Integer quertType,
                               @ApiParam(value = "支付类型（0全部，1微信，2支付宝，3会员，4通用通）", required = true) @RequestParam Integer orderType
    ) {
        SysUser user = getUserInfo();
        if (user.getType() != 3) {//只限商户用户登陆查询
            return Result.fail("没有查询权限");
        }
        Map result = new HashMap();

        //交易汇总信息
        Map statistics = orderService.apiStatisticsByMerchant(user.getParentId(), queryTime, quertType, orderType);
        result.put("statistics", statistics);
        //门店分组汇总列表数据
        IPage<Map> storeGroupPage = orderService.apiStatisticsListByMerchant(pageNum, pageSize, user.getParentId(), queryTime, quertType, orderType);
        result.put("page", storeGroupPage);
        return Result.success(result);
    }


    @ApiOperation(value = "查询店员分组汇总列表", notes = "查询店员分组汇总列表", httpMethod = "GET")
    @RequestMapping(value = "/getClerkOrderGroupList/{pageNum}/{pageSize}", method = RequestMethod.GET)
    @ResponseBody
    public Result getOrderList(@ApiParam(value = "当前页", required = true) @PathVariable("pageNum") Integer pageNum,
                               @ApiParam(value = "每页大小", required = true) @PathVariable("pageSize") Integer pageSize,
                               @ApiParam(value = "查询日期（2011-01-20）", required = true) @RequestParam String queryTime,
                               Long storeId,
                               @ApiParam(value = "查询类型（0日账单，1月账单，2年账单）", required = true) @RequestParam Integer quertType,
                               @ApiParam(value = "支付类型（0全部，1微信，2支付宝，3会员，4通用通）", required = true) @RequestParam Integer orderType
    ) {
        Long storeUserId = null;
        SysUser user = getUserInfo();
        if (user.getType() == 3) {//商户账号登陆
            if (storeId == null) {
                return Result.fail("请选择具体门店");
            }
        } else if (user.getType() == 4) {//门店账号登录
            storeId = user.getParentId();
            storeUserId = user.getId();
        } else {
            return Result.fail("没有查询权限");
        }
        Map result = new HashMap();
        //交易汇总信息
        Map statistics = orderService.apiStatisticsByStore(storeId, storeUserId, queryTime, quertType, orderType);
        result.put("statistics", statistics);
        //店员分组汇总列表数据
        IPage<Map> clerkGroupPage = orderService.apiStatisticsListByStore(storeId, storeUserId, pageNum, pageSize, queryTime, quertType, orderType);
        result.put("page", clerkGroupPage);
        return Result.success(result);
    }


    @ApiOperation(value = "查询店员交易列表", notes = "查询店员交易列表", httpMethod = "GET")
    @RequestMapping(value = "/getClerkOrderList/{pageNum}/{pageSize}", method = RequestMethod.GET)
    @ResponseBody
    public Result getClerkOrderList(@ApiParam(value = "当前页", required = true) @PathVariable("pageNum") Integer pageNum,
                                    @ApiParam(value = "每页大小", required = true) @PathVariable("pageSize") Integer pageSize,
                                    @ApiParam(value = "查询日期（2011-01-20）", required = true) @RequestParam String queryTime,
                                    Long storeUserId,
                                    @ApiParam(value = "查询类型（0日账单，1月账单，2年账单）", required = true) @RequestParam Integer quertType,
                                    @ApiParam(value = "支付类型（0全部，1微信，2支付宝，3会员，4通用通）", required = true) @RequestParam Integer orderType
    ) {
        SysUser user = getUserInfo();
        if (user.getType() == 3) {//商户账号登陆
            if (storeUserId == null) {
                return Result.fail("请选择店员");
            }
        } else if (user.getType() == 4) {//门店账号登录
            storeUserId = user.getId();
        } else {
            return Result.fail("没有查询权限");
        }
        Map result = new HashMap();
        //交易汇总信息
        Map statistics = orderService.apiStatisticsByStoreUser(storeUserId, queryTime, quertType, orderType);
        result.put("statistics", statistics);
        //店员分组汇总列表数据
        IPage<TOrder> clerkGroupPage = orderService.apiStatisticsListByStoreUser(storeUserId, pageNum, pageSize, queryTime, quertType, orderType);
        result.put("page", clerkGroupPage);
        return Result.success(result);
    }

    @ApiOperation(value = "打印指定订单", notes = "打印指定订单", httpMethod = "GET")
    @RequestMapping(value = "/printOrder", method = RequestMethod.GET)
    @ResponseBody
    public Result printOrder(@ApiParam(value = "订单ID", required = true) @RequestParam Long orderId) {
        if (orderId == null) {
            return Result.fail("请选择订单打印");
        }
        try {
            TOrder order = orderService.getById(orderId);
            if (order == null || order.getStoreUserId() == null) {
                return Result.fail("订单信息异常！");
            }
            SysUser user = sysUserService.getById(order.getStoreUserId());
            if (user == null || StringUtils.isBlank(user.getRegistrationId())) {
                return Result.fail("订单信息异常！");
            }
            JiGuangUtils.sendPushNotice(user.getRegistrationId(), "打印订单", order.getOutTradeNo());
            return Result.success();
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

}
